﻿using MaterialDesignThemes.Wpf;
using RPAVIEW.util;
using RPAVIEW.views.controlLibrary;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace RPAVIEW.views.EditPage.flows.NewPivotTableN {
    /// <summary>
    /// Input.xaml 的交互逻辑
    /// </summary>
    public partial class Input:BaseParamViewAction{
        public Input() {
            InitializeComponent();
            NewPivotTableNParam param = ParamUtil.GetCurrentParam<NewPivotTableNParam>();
            List<string> valueAndCalTypes = param.ValueAndCalTypes;
            foreach(string valueAndCalType in valueAndCalTypes){
                string[] vac = valueAndCalType.Split(new string[] {"==="}, StringSplitOptions.RemoveEmptyEntries);
                StackPanel sp = this.GetValueAndCalType();
                TextBox valuename = sp.Children[0] as TextBox;
                ComboBox cal = sp.Children[2] as ComboBox;
                valuename.Text = vac[0];
                cal.SelectedIndex = int.Parse(vac[1]);
                this.values.Children.Add(sp);
            }
        }

        public override void UpdateSource() {
            this.excelObject.GetBindingExpression(SelVarTextBox.text).UpdateSource();
            this.sourceSheet.GetBindingExpression(SelVarTextBox.text).UpdateSource();
            this.targetSheet.GetBindingExpression(SelVarTextBox.text).UpdateSource();
            this.targetRow.GetBindingExpression(SelVarTextBox.text).UpdateSource();
            this.targetColumn.GetBindingExpression(SelVarTextBox.text).UpdateSource();
            this.piovtName.GetBindingExpression(SelVarTextBox.text).UpdateSource();
            this.rowFields.GetBindingExpression(SelVarTextBox.text).UpdateSource();
            this.columnFields.GetBindingExpression(SelVarTextBox.text).UpdateSource();
            NewPivotTableNParam param = ParamUtil.GetCurrentParam<NewPivotTableNParam>();
            List<string> valueAndCalTypes = new List<string>();
            foreach(StackPanel child in this.values.Children) {
                SelVarTextBox nameText = child.Children[0] as SelVarTextBox;
                ComboBox calType = child.Children[2] as ComboBox;
                string valueName = nameText.Text;
                int index = calType.SelectedIndex;
                valueAndCalTypes.Add(valueName + "===" + index);
            }
            param.ValueAndCalTypes = valueAndCalTypes;
        }

        public void RemoveValue(object sender, RoutedEventArgs e) {
            Button button = sender as Button;
            this.values.Children.Remove(button.Parent as StackPanel);
        }

        public void AddValueAndCalType(object sender, RoutedEventArgs e) {
            StackPanel v = this.GetValueAndCalType();
            this.values.Children.Add(v);
        }

        private StackPanel GetValueAndCalType() {
            StackPanel con = new StackPanel();
            con.Orientation = Orientation.Horizontal;

            SelVarTextBox valueText = new SelVarTextBox();
            ComboBox calCombox = new ComboBox();
            Label label = new Label();
            
            con.Children.Add(valueText);
            con.Children.Add(label);
            con.Children.Add(calCombox);

            valueText.Padding = new Thickness(1, 4, 1, 1);
            valueText.Width = 250;
            valueText.Hint = "值属性";

            calCombox.Style = (Style)this.FindResource("LongComboBoxStyle");
            calCombox.Width = 185;
            HintAssist.SetHint(calCombox, "计算方式");
            ComboBoxItem count = new ComboBoxItem();
            ComboBoxItem sum = new ComboBoxItem();
            count.Content = "计数";
            sum.Content = "求和";
            calCombox.Items.Add(count);
            calCombox.Items.Add(sum);

            label.Content = " / ";
            label.Margin = new Thickness(0, 20, 0, 0);
            label.Style = (Style)this.FindResource("LableFontStyle");

            Button button = new Button();
            button.Width = 30;
            button.Height = 20;
            button.Style = (Style)this.FindResource("MaterialDesignRaisedLightButton");
            button.ToolTip = "删除值";
            button.HorizontalAlignment = HorizontalAlignment.Left;
            button.VerticalAlignment = VerticalAlignment.Bottom;
            button.Margin = new Thickness(5, 0, 0, 5);
            button.Padding = new Thickness(0, 0, 0, 0);
            button.Click += new RoutedEventHandler(RemoveValue);
            con.Children.Add(button);

            PackIcon icon = new PackIcon();
            icon.Kind = PackIconKind.Minus;
            button.Content = icon;
            return con;
        }
    }
}
